package com.angel.controller;

import com.angel.bean.LearnExp;
import com.angel.bean.VipInfo;
import com.angel.service.LearnExpService;
import com.angel.service.VipInfoService;
import com.angel.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看个人心得
 */
@Component
@RequestMapping("/e")
public class UserExperienceContoller {


    @Autowired
    private LearnExpService learnExpService;
    /**
     * 查看心得
     * @param model 容器
     * @return
     */
    @RequestMapping("/experience")
    public String experience(Model model,@RequestParam(required = false) String pageNum){
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pageNum);
        } catch (Exception e) {
            pageNo =1;
        }

       Integer userId = UserUtils.getPrincipal().getUserInfo().getId();
        try {
            LearnExp learnExp=new LearnExp();
            learnExp.setUid(userId);//由会员id来查询
            PageHelper.startPage(pageNo, 6);
            List<LearnExp> relsut_learn=learnExpService.findAllByuid(learnExp);//查询所有心得
            PageInfo<LearnExp> page = new PageInfo<>(relsut_learn);
            if (relsut_learn!=null){
                model.addAttribute("emp",true);
                model.addAttribute("pageInfo",page);
            }else {
                model.addAttribute("emp",false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "experience";
    }

    /**
     * 删除心得
     * @param id 要删除的心得的id
     * @return
     */
    @RequestMapping("/delet")
    @ResponseBody
    public Object delet(Integer id){
        Map<String,String> map=new HashMap<>();
        LearnExp learnExp=new LearnExp();
        learnExp.setId(id);
        int result=learnExpService.deletId(learnExp);
        return map;
    }
}
